#!/bin/sh
#->
#  Name:		install
VERSION='0.5'
#  Modified:	Sun Nov 21 2010, 23:52:40
#  Insects:		What species? No bugs known,yet.

#  Feel free to contact me at:
#  <http://sites.google.com/site/code933k>
#  in case of bugs, updates, proposals or comments

#  Copyright (C) 2009 Mario García H.
 
#  "THE BEER-WARE LICENSE" (Revision 42):
#  <code933k (AT) gmail> wrote this file. As long as you retain
#  this notice you can do whatever you want with this stuff.

#  If we meet some day, and you think this stuff is worth it,
#  you can buy me a BEER in return, Mario García H.

#  THIS IS FREE SOFTWARE
#  THERE IS NO WARRANTY, TO THE EXTENT PERMITTED BY LAW.
#<-

test -z "$DESTDIR"	&& DESTDIR=""
test -z "$LIB"		&& LIB="$DESTDIR/usr/lib/linuxrules" || LIB="${DESTDIR}${LIB}/linuxrules"
test -z "$BIN"		&& BIN=/usr/bin
test -z "$TMP"		&& TMP="$PWD"
test -z "$CREATE"   && CREATE=true

RULES=''
LOC="$LIB"


function install_error(){
	printf "\033[01;31mERROR:  \033[m$*\n"
	exit 1
}

function create_dirs(){
	for dir in $@
	do
		install -d -m0755 "$dir" 2>/dev/null \
		|| install_error "Cannot create $dir. CHECK USER PERMISSION"
	done
	return 0
}


# INSTALL
# help
case $1 in
	-h|h|-help|--help|help)
    cat <<EOF

Simple install.sh $VERSION, for linuxrules

usage: [PATH=/...] ./install.sh'

Variable paths:
DESTDIR:  For Linux distro installers as rpm or deb.'
LIB:      System default library dir or user set'
BIN:      System default bin dir or user space if supported'
DOC:      Install general docs if this variable exists and'
          points to a writable directory'
TMP:      Volatile dir, just for making linuxrules'
          executable file.'

NOTICE:
IF YOU WANT TO CREATE AN RPM/DEB OR SIMILAR PACKAGE,
MAKE SURE YOU RUN INSTALL WITH CREATE=false:
CREATE=false ./install.sh
You don't want linuxrules to create dynamic config
for your server instead of the user machine..!

# THIS IS FREE SOFTWARE'
# THERE IS NO WARRANTY, TO THE EXTENT PERMITTED BY LAW.'
Copyright (C) 2009 Mario García H.'

License Beerware (Revision 42)

EOF

        exit 0
	;;
    -l|--local)
        LOC=$HOME/.local/share/linuxrules
        BIN=$HOME/bin
        LIB=$LOC/lib
        DOC=$LOC/doc
    ;;    
esac

# install
create_dirs "$BIN" "$LIB" "$TMP" "$LIB/helpers"

## ----- CREATE SENSORS LIBRARY 1st ----- ##
if test "$CREATE"
then
    chmod +x $PWD/helpers/create_temps.sh \
    && sh -c $PWD/helpers/create_temps.sh \
    > $PWD/lib/sensors.awk \
    || install_error "Cannot install dynamic libraries. CHECK USER PERMISSION"
fi

for library in $PWD/lib/*.awk
do
	install -m0644 $library $LIB
done

## -----  REMOVE REMANANT  -------------- ##
rm -f $PWD/lib/*sensors.awk
## -------------------------------------- ##

if test -n "$DOC"
then
	create_dirs "$DOC/linuxrules"
	install -m0644 $PWD/doc/{README,COPYING,ChangeLog,BUGS,TODO} \
	"$DOC/linuxrules"
fi

for helper in $PWD/helpers/*.*
do
	install -m0755 $helper $LIB/helpers 2>/dev/null \
	|| install_error "Cannot install $helper. CHECK USER PERMISSION"
done

for rule in $LIB/*.awk
do
    # Output rule should be the last one
    # this is a provisional workaround to
    # achieve that.
    if test $rule != "$LIB/output.awk"
    then
        RULES="$RULES -f $rule"
    fi
done

RULES="$RULES -f $LIB/output.awk"

rm -f $TMP/linuxrules
rm -f $BIN/linuxrules

echo "exec awk$RULES" > $TMP/linuxrules
install -m0755 $TMP/linuxrules $BIN/ 2>/dev/null \
|| install_error "Cannot install linuxrules to $BIN, CHECK USER PERMISSION"

rm -f $TMP/linuxrules
echo $BIN/linuxrules > '.install.rec'
echo $LIB >>'.install.rec'
echo $DOC >>'.install.rec'

if echo "$LIB" | grep "$LOC" >/dev/null
then
    echo "$LOC" >> '.install.rec'
fi

echo Install locations:
echo ------------------
cat '.install.rec'
echo ------------------
echo
echo 'Type "linuxrules" to start using it!'

exit 0
